const net = require('net');
const http = require('http')
const https = require('https')


// net.createServer(function(sock) {
//   sock.on('data', function(data) {
//     sock.write('HTTP/1.1 200 OK\r\n');
//     sock.write('Transfer-Encoding: chunked\r\n');
//     sock.write('\r\n');
//
//     sock.write('b\r\n');
//     sock.write('01234567890\r\n');
//
//     sock.write('5\r\n');
//     sock.write('12345\r\n');
//
//     setTimeout(function () {
//       sock.write('0\r\n');
//       sock.write('\r\n');
//     }, 2000);
//   });
// }).listen(9090, '127.0.0.1');

// const url = 'https://www.7-zip.org/a/7z1900.exe';
const url = 'https://npm.taobao.org/mirrors/node/v14.7.0/node-v14.7.0-win-x64.zip';
const parsed = new URL(url);
const method = 'get';
let h;
if (parsed.protocol === 'https:') {
  h = https
} else if (parsed.protocol === 'http:') {
  h = http
} else {
  throw new Error(`Unknown protocol, ${parsed.protocol}`)
}
const request = {
  path: parsed.pathname + parsed.search,
  port: parsed.port,
  method: method,
  headers: {},
  hostname: parsed.hostname
}
const req = h.request(request, (res) => {
  console.log('状态码:', res.statusCode);
  console.log('请求头:', res.headers);

  // res.on('data', (d) => {
    // process.stdout.write(d);
  // });
});

req.on('error', (e) => {
  console.error(e);
});
req.end();
